package com.yml.mall.shipping;

import com.yml.mall.enums.ResponseEnum;
import com.yml.mall.shipping.dao.ShippingMapper;
import com.yml.mall.shipping.pojo.InsertAndUpdateDTO;
import com.yml.mall.shipping.pojo.ShippingDO;
import com.yml.mall.shipping.pojo.ShippingDTO;
import com.yml.mall.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName ShippingServiceImpl
 * @Author Administrator
 * @Date 2020/4/10   17:57
 */
@Service
@Slf4j
public class ShippingServiceImpl implements ShippingService {
    private  final ShippingMapper shippingMapper;
    @Autowired
    public ShippingServiceImpl(ShippingMapper shippingMapper){
        this.shippingMapper=shippingMapper;
    }

    @Override
    public ResponseVO<Map<String,Integer>> addShipping(InsertAndUpdateDTO insertAndUpdateDTO) {
        ShippingDO shippingDO =new ShippingDO();
        BeanUtils.copyProperties(insertAndUpdateDTO,shippingDO);
        Date date=new Date(System.currentTimeMillis());

        shippingDO.setCreateTime(date).setUpdateTime(date);
        int re= shippingMapper.insertSelective(shippingDO);
        if(re!=1)
            return ResponseVO.error(ResponseEnum.ERROR);
        shippingDO=shippingMapper.selectByCreateTime(date);

        if(shippingDO==null)
            return ResponseVO.error(ResponseEnum.ERROR);
        Map<String,Integer> data= new HashMap<String,Integer>();
        data.put("shippingId",shippingDO.getId());
        return ResponseVO.success(data);

    }

    @Override
    public ResponseVO deleteShipping(Integer shippingId,Integer userId) {
        ResponseVO<ShippingDO> responseVO = judge(shippingId,userId);
        if(!responseVO.getStatus().equals(0)) return  responseVO;

        int re=shippingMapper.deleteByPrimaryKey(shippingId);
        if(re!=1)
            return ResponseVO.error(ResponseEnum.ERROR);
        return ResponseVO.success();
    }

    @Override
    public ResponseVO updateShipping(Integer shippingId,InsertAndUpdateDTO insertAndUpdateDTO, Integer userId) {
        ResponseVO<ShippingDO> responseVO = judge(shippingId,userId);
        if(!responseVO.getStatus().equals(0)) return  responseVO;

        BeanUtils.copyProperties(insertAndUpdateDTO,responseVO.getData());
        int re =shippingMapper.updateByPrimaryKeySelective(responseVO.getData());
        if(re!=1) return  ResponseVO.error(ResponseEnum.ERROR);
        return ResponseVO.success();
    }

    @Override
    public ResponseVO<ShippingDTO> detail(Integer shippingId, Integer userId) {
        ResponseVO responseVO=judge(shippingId,userId);
        ShippingDTO shippingDTO =new ShippingDTO();
        if(responseVO.getStatus().equals(0)) {
            BeanUtils.copyProperties( responseVO.getData(),shippingDTO);
            return ResponseVO.success(shippingDTO);
        }
        return responseVO;
    }

    @Override
    public ResponseVO<List<ShippingDTO>> shippingList(Integer userId) {
        List<ShippingDO> shippingDOList = shippingMapper.selectByUserId(userId);
        List<ShippingDTO> shippingDTOList =new ArrayList<>();
        for(int i=0;i<shippingDOList.size();i++){
            ShippingDTO shippingDTO =new ShippingDTO();
            BeanUtils.copyProperties(shippingDOList.get(i),shippingDTO);
            shippingDTOList.add(shippingDTO);
        }
        return ResponseVO.success(shippingDTOList);
    }

    //检查user 和 shipping 的对应关系
    private ResponseVO<ShippingDO>  judge(Integer shippingId,Integer userId){
        ShippingDO shippingDO = shippingMapper.selectByPrimaryKey(shippingId);
        if(shippingDO == null)
            return ResponseVO.error(ResponseEnum.DATA_NOT_EXIST);
        if(!shippingDO.getUserId().equals(userId))
            return ResponseVO.error(ResponseEnum.USER_INFO_ERROR);
        return ResponseVO.success(shippingDO);
    }



}
